clear all
clc

figure('name','模拟电路');
axis([-3,12,0,10]);
hold on
axis('off');

fill([-1.5,-1.5,1.5,1.5],[1,5,5,1],[0.5,1,1]);
fill([-0.5,-0.5,0.5,0.5],[5,5.5,5.5,5],[0,0,0]);
text(-0.5,1.5,'—');
text(-0.5,3,'蓄电池');
text(-0.5,4.5,'+');

plot([0;0],[5.5;6.7],'color','r','linestyle','-','linewidth',4);
plot([0;4],[6.7;6.7],'color','r','linestyle','-','linewidth',4);
a = line([4;5],[6.7;7.7],'color','b','linestyle','-','linewidth',4,'erasemode','xor');

plot([5.2;9.2],[6.7;6.7],'color','r','linestyle','-','linewidth',4);
plot([9.2;9.2],[6.7;3.7],'color','r','linestyle','-','linewidth',4);
plot([9.2;9.7],[3.7,3.7],'color','r','linestyle','-','linewidth',4);
plot([0;0],[1;0],'color','r','linestyle','-','linewidth',4);
plot([0;10],[0;0],'color','r','linestyle','-','linewidth',4);
plot([10;10],[0;3],'color','r','linestyle','-','linewidth',4);

fill([9.8,10.2,9.7,10.3],[3,3,3.3,3.3],[0,0,0]);
plot([9.7,9.7],[3.3,4.3],'color','b','linestyle','-','linewidth',0.5);
plot([10.3,10.3],[3.3,4.45],'color','b','linestyle','-','linewidth',0.5);

x = 9.7:pi/50:10.3;
plot(x,4.3+0.1*sin(40*pi*(x-9.7)),'color','b','linestyle','-','linewidth',0.5);
t = 0:pi/60:2*pi;
plot(10+0.7*cos(t),4.3+0.6*sin(t),'color','b');

text(4.5,10,'电流运动方向');
line([4.5;6.6],[9.4;9.4],'color','r','linestyle','-','linewidth',4,'erasemode','xor');
line(6.7,9.4,'color','b','linestyle','-','erasemode','xor','markersize',10);
pause(1);

t = 0;
y = 7.6;
while y > 6.6
    x = 4 + sqrt(2)*cos(pi/4*(1-t));
    y = 6.7 + sqrt(2)*sin(pi/4*(1-t));
    set(a,'xdata',[4;x],'ydata',[6.7;y]);
    drawnow;
    t = t + 0.1;
end

pause(1);

light = line(10,4.3,'color','y','marker','.','markersize',40,'erasemode','xor');
h = line([1;1],[5.2;5.6],'color','r','linestyle','-','linewidth',4,'erasemode','xor');
g = line(1,5.7,'color','b','linestyle','-','erasemode','xor','markersize',10);

t = 0;
m2 = 5.6;
n = 5.6;
while n < 6.5;
    m = 1;
    n = 0.05*t + 5.6;
    set(h,'xdata',[m;m],'ydata',[n-0.5;n-0.1]);
    set(g,'xdata',m,'ydata',n);
    t = t + 0.01;
    drawnow;
end

t = 0;
while t < 1;
    m = 1.2 - 0.2*cos((pi/4)*t);
    n = 6.3 + 0.2*sin((pi/4)*t);
    set(h,'xdata',[m-0.5;m-0.1],'ydata',[n;n]);
    set(g,'xdata',m,'ydata',n);
    t = t + 0.05;
    drawnow;
end

t = 0;
while t < 0.4
     t = t + 0.5;
     g = line(1.2,6.5,'color','b','linestyle','^','markersize',10,'erasemode','xor');
     g = line(1.2,6.5,'color','b','linestyle','>','markersize',10,'erasemode','xor');
     set(g,'xdata',1.2,'ydata',6.5);
     drawnow;
end
